<template>
  <el-dialog
    :title="!dataForm.smsMshopId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="pmsProductId">
      <el-input v-model="dataForm.pmsProductId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="pmsProductName">
      <el-input v-model="dataForm.pmsProductName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="pmsProductPrice">
      <el-input v-model="dataForm.pmsProductPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="商品秒杀价" prop="smsMshopPrice">
      <el-input v-model="dataForm.smsMshopPrice" placeholder="商品秒杀价"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="smsMshopRepertory">
      <el-input v-model="dataForm.smsMshopRepertory" placeholder="库存"></el-input>
    </el-form-item>
    <el-form-item label="限量/人" prop="smsMshopXianliang">
      <el-input v-model="dataForm.smsMshopXianliang" placeholder="限量/人"></el-input>
    </el-form-item>
    <el-form-item label="时间表id" prop="smsMdateId">
      <el-input v-model="dataForm.smsMdateId" placeholder="时间表id"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="smsMdateStartDate">
      <el-input v-model="dataForm.smsMdateStartDate" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="smsMdateEndDate">
      <el-input v-model="dataForm.smsMdateEndDate" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="状态（1.未开始2.活动开始3.活动结束）" prop="smsMshopStatus">
      <el-input v-model="dataForm.smsMshopStatus" placeholder="状态（1.未开始2.活动开始3.活动结束）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          smsMshopId: 0,
          pmsProductId: '',
          pmsProductName: '',
          pmsProductPrice: '',
          smsMshopPrice: '',
          smsMshopRepertory: '',
          smsMshopXianliang: '',
          smsMdateId: '',
          smsMdateStartDate: '',
          smsMdateEndDate: '',
          smsMshopStatus: ''
        },
        dataRule: {
          pmsProductId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          pmsProductName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          pmsProductPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          smsMshopPrice: [
            { required: true, message: '商品秒杀价不能为空', trigger: 'blur' }
          ],
          smsMshopRepertory: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          smsMshopXianliang: [
            { required: true, message: '限量/人不能为空', trigger: 'blur' }
          ],
          smsMdateId: [
            { required: true, message: '时间表id不能为空', trigger: 'blur' }
          ],
          smsMdateStartDate: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          smsMdateEndDate: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          smsMshopStatus: [
            { required: true, message: '状态（1.未开始2.活动开始3.活动结束）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.smsMshopId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.smsMshopId) {
            this.$http({
              url: this.$http.adornUrl(`/tgou/miaoshop/info/${this.dataForm.smsMshopId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pmsProductId = data.miaoShop.pmsProductId
                this.dataForm.pmsProductName = data.miaoShop.pmsProductName
                this.dataForm.pmsProductPrice = data.miaoShop.pmsProductPrice
                this.dataForm.smsMshopPrice = data.miaoShop.smsMshopPrice
                this.dataForm.smsMshopRepertory = data.miaoShop.smsMshopRepertory
                this.dataForm.smsMshopXianliang = data.miaoShop.smsMshopXianliang
                this.dataForm.smsMdateId = data.miaoShop.smsMdateId
                this.dataForm.smsMdateStartDate = data.miaoShop.smsMdateStartDate
                this.dataForm.smsMdateEndDate = data.miaoShop.smsMdateEndDate
                this.dataForm.smsMshopStatus = data.miaoShop.smsMshopStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tgou/miaoshop/${!this.dataForm.smsMshopId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'smsMshopId': this.dataForm.smsMshopId || undefined,
                'pmsProductId': this.dataForm.pmsProductId,
                'pmsProductName': this.dataForm.pmsProductName,
                'pmsProductPrice': this.dataForm.pmsProductPrice,
                'smsMshopPrice': this.dataForm.smsMshopPrice,
                'smsMshopRepertory': this.dataForm.smsMshopRepertory,
                'smsMshopXianliang': this.dataForm.smsMshopXianliang,
                'smsMdateId': this.dataForm.smsMdateId,
                'smsMdateStartDate': this.dataForm.smsMdateStartDate,
                'smsMdateEndDate': this.dataForm.smsMdateEndDate,
                'smsMshopStatus': this.dataForm.smsMshopStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
